<?php
App::uses('AppController', 'Controller');
/**
 * TicketBooks Controller
 *
 * @property TicketBook $TicketBook
 * @property PaginatorComponent $Paginator
 */
class TicketBooksController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	public function beforeFilter(){
		$this->Auth->allow('add');
		parent::beforeFilter();
	}
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->TicketBook->recursive = 0;
		$action = array('admin' => true,'controller' => 'TicketBooks','action'=>'jsonPage');
		$param = $this->TicketBook->pageParam($action);
		$this->set('paginationScript',$this->TicketBook->showGridPagination($param));
	}
	
	/**
	 * function
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 下午5:05:53
	 * @param variable
	 * @return
	 */
	function admin_jsonPage($type = false){
		$this->TicketBook->recursive = 0;
		$this->autoRender = false;
		$searchtype = $this->request->data('_search');
		$conditions = array();
		if($searchtype=='true'){
			$conditions = $this->search_condition($this->request->data);
		}
		
		$conditions['TicketBook.sys_website_id']= $this->Session->read('SITEID');
	
		$config = array(
				'_search'=>$searchtype,
				'page'=>$this->request->data('page'),
				'rows'=>$this->request->data('rows'),
				'sidx'=>$this->request->data('sidx'),
				'sord'=>$this->request->data('sord')
		);
		$res = $this->TicketBook->pagination($config,array(),$conditions);
		return $this->TicketBook->jsonData($res);
	}
	/**
	 * 查询条件
	 * @author zmx@gootop.net
	 * @Date 2011-2-23
	 * @return
	 */
	private function search_condition($param){
		 return array();
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->TicketBook->exists($id)) {
			throw new NotFoundException(__('Invalid ticket book'));
		}
		$options = array('conditions' => array('TicketBook.' . $this->TicketBook->primaryKey => $id));
		$this->set('ticketBook', $this->TicketBook->find('first', $options));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->TicketBook->exists($id)) {
			throw new NotFoundException(__('Invalid ticket book'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->TicketBook->save($this->request->data)) {
				$this->Session->setFlash(__('The ticket book has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ticket book could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('TicketBook.' . $this->TicketBook->primaryKey => $id));
			$this->request->data = $this->TicketBook->find('first', $options);
		}
		$sysWebsites = $this->TicketBook->SysWebsite->find('list');
		$ticketPrices = $this->TicketBook->TicketPrice->find('list');
		$users = $this->TicketBook->User->find('list');
		$this->set(compact('sysWebsites', 'ticketPrices', 'users'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->TicketBook->id = $id;
		if (!$this->TicketBook->exists()) {
			throw new NotFoundException(__('Invalid ticket book'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->TicketBook->delete()) {
			$this->Session->setFlash(__('The ticket book has been deleted.'));
		} else {
			$this->Session->setFlash(__('The ticket book could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	
	public function admin_state($id = null) {
		$this->TicketBook->id = $id;
		if (!$this->TicketBook->exists()) {
			throw new NotFoundException(__('Invalid ticket'));
		}
		//$this->request->onlyAllow('post', 'delete');
		$this->TicketBook->set('enable',2);
		if ($this->TicketBook->save()) {
			$this->Session->setFlash(__('The ticket has been processed.'),'tip/tip_float',array('t'=>'success'));
		} else {
			$this->Session->setFlash(__('The ticket could not be processed. Please, try again.'),'tip/tip_float');
		}
		return $this->redirect(array('action' => 'index'));
	}

	public function index(){
		$this->Paginator->settings = array(
				'conditions'=>	array('TicketBook.user_id'=>$this->Auth->user('id')),
				'order'=>'TicketBook.created DESC',
				'limit'=>7
		);
		$this->TicketBook->recursive = 2;
		$ticketBooks = $this->Paginator->paginate('TicketBook');
		$this->set('ticketBooks',$ticketBooks);
	}
	
	/**
	 * admin_add method
	 *
	 * @return void
	 */
	public function add() {
		if ($this->request->is('post')) {
			$this->TicketBook->create();
			$this->request->data['TicketBook']['sys_website_id'] = Configure::read('SITEID');
			if ($this->TicketBook->save($this->request->data)) {
				$this->set('state',true);
				$this->set('msg','提交成功 我们将尽快回复');
				
			} else {
				$this->set('msg','保存失败 请重试');
			}
		}
	}
}
